package com.zzsy.carosshopprojects.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.king.base.util.ToastUtils;
import com.yatoooon.screenadaptation.ScreenAdapterTools;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.util.MoneyEditUtils;
import com.zzsy.carosshopprojects.util.StringUtils;

import androidx.annotation.NonNull;


/**
 * 带有两个按钮的dialog
 * Created on 2018/3/21.
 */
public class ComTwnBtnReceiptDialog extends Dialog implements View.OnClickListener {

    private Activity activity;

    private TextView titleTv;
    private TextView leftBtn;
    private TextView rightBtn;
    private String banalce;

    private IDialogBtnClickListener dialogBtnClickListener;
    private EditText etView;

    public interface IDialogBtnClickListener {
        void onDialogLeftBtnClick();

        void onDialogRightBtnClick(String money);
    }

    public ComTwnBtnReceiptDialog(@NonNull Activity context, String contText) {
        this(context, R.style.BottomPopupDialog,contText);
    }

    public ComTwnBtnReceiptDialog(@NonNull Activity context, int themeResId, String contText) {
        super(context, themeResId);
        this.activity = context;
        this.banalce = contText;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        setContentView(R.layout.dialog_com_receipt_btn);
        ScreenAdapterTools.getInstance().loadView(getWindow().getDecorView());
        initUI();
    }

    private void initUI() {
        titleTv = findViewById(R.id.tip_tv);
        leftBtn = findViewById(R.id.left_btn);
        rightBtn = findViewById(R.id.right_btn);
        etView = findViewById(R.id.et_view);
        titleTv.setText("剩余余额:"+banalce);
        MoneyEditUtils.afterDotTwo(etView);
        titleTv.setOnClickListener(this);
        leftBtn.setOnClickListener(this);
        rightBtn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_btn:
                leftBtn.setClickable(false);
                leftBtn.setFocusable(false);
                dialogBtnClickListener.onDialogLeftBtnClick();
                dismiss();
                break;

            case R.id.right_btn:
                rightBtn.setClickable(false);
                rightBtn.setFocusable(false);
                String money = etView.getText().toString();

                if(StringUtils.isEmpty(money)){
                    ToastUtils.showToast(activity.getApplicationContext(),"请输入金额");
                    rightBtn.setClickable(true);
                    rightBtn.setFocusable(true);
                    return;
               }
                if(!StringUtils.isEmpty(money)) {
                    if (Double.parseDouble(money) > Double.parseDouble(banalce)) {
                        ToastUtils.showToast(activity.getApplicationContext(),"您的开票额度不足！");
                        rightBtn.setClickable(true);
                        rightBtn.setFocusable(true);
                        return;
                    }
                }
                dialogBtnClickListener.onDialogRightBtnClick(money);
                dismiss();
                break;

            default:
                break;
        }
    }

    @Override
    public void show() {
        super.show();
        Window window = getWindow();
        window.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画
        WindowManager.LayoutParams layoutParams = activity.getWindow().getAttributes();
        layoutParams.alpha = 0.5f;
        layoutParams.dimAmount = 0.5f;
        activity.getWindow().setAttributes(layoutParams);

    }

    @Override
    public void dismiss() {
        super.dismiss();

        WindowManager.LayoutParams layoutParams = activity.getWindow().getAttributes();
        layoutParams.alpha = 1.0f;
        layoutParams.dimAmount = 1.0f;
        activity.getWindow().setAttributes(layoutParams);
    }



    public void setDialogBtnClickListener(IDialogBtnClickListener dialogBtnClickListener) {
        this.dialogBtnClickListener = dialogBtnClickListener;
    }
}